package com.ymtms.statisticalform.service.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ymtms.statisticalform.dao.GoodsbillMasterMapper;
import com.ymtms.statisticalform.pojo.CollectingMoneyBalance;
import com.ymtms.statisticalform.pojo.GoodsbillMaster;
import com.ymtms.statisticalform.pojo.MoneyFreight;
import com.ymtms.statisticalform.service.MoneyFreightService;
import com.ymtms.statisticalform.util.TimeRangeUtil;
import com.ymtms.statisticalform.vo.GridRequest;
import com.ymtms.statisticalform.vo.GridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class MoneyFreightServiceImpl implements MoneyFreightService {
    @Autowired
    private GoodsbillMasterMapper goodsbillMasterMapper;

    @Override
    public List<MoneyFreight> selectAllCarriageRanking(String year,String month) {
        List<MoneyFreight> moneys = goodsbillMasterMapper.selectAllCarriageRanking(year,month);
        return moneys;
    }

    @Override
    public List<GoodsbillMaster> selectBusinessIncomeByMonth(String year,String month) {
        List<GoodsbillMaster> moneys = goodsbillMasterMapper.selectBusinessIncomeByMonth(year,month);
        return moneys;
    }

    @Override
    public GridResult<GoodsbillMaster> queryByPage(GridRequest gridRequest, String factDealDateString, TimeRangeUtil balanceDateRange) {
        // 获取当前页和页大小
        Integer currentPage = gridRequest.getPage();
        Integer pageSize = gridRequest.getLimit();

        // 判断是否有排序,有则生成符合pagehelper的排序字符串
        String sidx = gridRequest.getSidx(); // 排序参数
        String sord = gridRequest.getSord(); // 排序规则

        System.out.println(currentPage);
        System.out.println(pageSize);
        System.out.println(sidx);
        System.out.println(sord);

        String orderBy = "";
        // pagehelper静态设置页属性方法，它会拦截下面的第一个查询！第二个无效。
        if(checkOrderBy(sidx, sord)){
            sidx = upperCharToUnderLine(sidx);
            orderBy = sidx+" "+sord;

            System.out.println(orderBy);

            PageHelper.startPage(currentPage, pageSize, orderBy);
        }else{
            // 设置当前页和页大小
            PageHelper.startPage(currentPage, pageSize);
        }


        // 查询结果（传入排序）
        List<GoodsbillMaster> result = goodsbillMasterMapper.queryByPage(factDealDateString, balanceDateRange);
        System.out.println("查到的数据数量："+result.size());


        // 利用当前页和页大小配置pageinfo
        PageInfo<GoodsbillMaster> pageList = new PageInfo<>(result);
        System.out.println("pageList::"+pageList);
        // 新建一个封装类对象，设置参数，传入查询得到的数据
        GridResult<GoodsbillMaster> grid = new GridResult<>();

        // 存入查询数据
        grid.setData(result);
        // 存入总条数
        grid.setCount(pageList.getTotal());

        return grid;
    }

    /**
     * 客户费用排行，按条件查询
     * @param gridRequest
     * @param dateString
     * @return
     */
    @Override
    public GridResult<MoneyFreight> moneyFreightLay(GridRequest gridRequest, String year,String month) {
        // 获取当前页和页大小
        Integer currentPage = gridRequest.getPage();
        Integer pageSize = gridRequest.getLimit();

        // 判断是否有排序,有则生成符合pagehelper的排序字符串
        String sidx = gridRequest.getSidx(); // 排序参数
        String sord = gridRequest.getSord(); // 排序规则

        System.out.println(currentPage);
        System.out.println(pageSize);
        System.out.println(sidx);
        System.out.println(sord);

        String orderBy = "";
        // pagehelper静态设置页属性方法，它会拦截下面的第一个查询！第二个无效。
        if(checkOrderBy(sidx, sord)){
            sidx = upperCharToUnderLine(sidx);
            orderBy = sidx+" "+sord;

            System.out.println(orderBy);

            PageHelper.startPage(currentPage, pageSize, orderBy);
        }else{
            // 设置当前页和页大小
            PageHelper.startPage(currentPage, pageSize);
        }


        // 查询结果（传入排序）
        List<MoneyFreight> result = goodsbillMasterMapper.selectCarriageRankingByDate(year,month);
        System.out.println("查到的数据数量："+result.size());


        // 利用当前页和页大小配置pageinfo
        PageInfo<MoneyFreight> pageList = new PageInfo<>(result);
        System.out.println("pageList::"+pageList);
        // 新建一个封装类对象，设置参数，传入查询得到的数据
        GridResult<MoneyFreight> grid = new GridResult<>();

        // 存入查询数据
        grid.setData(result);
        // 存入总条数
        grid.setCount(pageList.getTotal());

        return grid;
    }

    // 判断是否非空
    private boolean checkOrderBy(String sidx,String sord){
        if(sidx != null && sord != null
                && sidx.trim().length() > 0
                && sord.trim().length() > 0){
            return true;
        }
        return false;
    }
    // 大写转换为下划线+小写
    public static String upperCharToUnderLine(String param) {
        Pattern p = Pattern.compile("[A-Z]");
        if (param == null || param.equals("")) {
            return "";
        }
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param);
        int i = 0;
        while (mc.find()) {
            System.out.println(builder.toString());
            System.out.println("mc.start():" + mc.start() + ", i: " + i);
            System.out.println("mc.end():" + mc.start() + ", i: " + i);
            builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
            i++;
        }

        if ('_' == builder.charAt(0)) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }
}
